package com.jubotech.framework.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.protobuf.ByteString;
import com.jubotech.framework.netty.utils.MessageUtil;
import com.jubotech.framework.netty.utils.MsgIdBuilder;
import com.jubotech.framework.netty.utils.NettyConnectionUtil;

import Jubo.JuLiao.IM.Wx.Proto.AcceptFriendAddRequestTask.AcceptFriendAddRequestTaskMessage;
import Jubo.JuLiao.IM.Wx.Proto.FriendAddReqeustNotice.FriendAddReqeustNoticeMessage;
import Jubo.JuLiao.IM.Wx.Proto.TalkToFriendTask.TalkToFriendTaskMessage;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.EnumContentType;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.EnumMsgType;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.TransportMessage;
import io.netty.channel.ChannelHandlerContext;

/**
 * @author: xiaoxx
 * @date: 2019/12/8 22:12
 */
public class MessageSendUtils {

    private static final Logger log = LoggerFactory.getLogger(MessageSendUtils.class);
    
    /**
     * 自动通过好友请求回复
     * @param vo
     * @param req
     * @param ctx
     */
    public static void throughFriendRequest(TransportMessage vo,FriendAddReqeustNoticeMessage req,ChannelHandlerContext ctx) {
    	AcceptFriendAddRequestTaskMessage.Builder  bd = AcceptFriendAddRequestTaskMessage.newBuilder();
		bd.setWeChatId(req.getWeChatId());
		bd.setFriendId(req.getFriendId());
		bd.setFriendNick(req.getFriendNick());
		bd.setOperationValue(1);//默认接受请求
		AcceptFriendAddRequestTaskMessage resp = bd.build();
		MessageUtil.sendMsg(ctx, EnumMsgType.AcceptFriendAddRequestTask, vo.getAccessToken(), vo.getId(), resp);
    }

    /**
     * 给指定用户发文字消息
     * @param weChatId
     * @param friendWeChatId
     * @param content
     */
    public static void sendMessage(String weChatId,String friendWeChatId,String content) {
        log.info("发送消息......{}",content);
        ByteString byteString = ByteString.copyFromUtf8(content);
        //当前发消息的微信的通道
        ChannelHandlerContext chx = NettyConnectionUtil.getClientChannelHandlerContextByUserId(weChatId);
        if (chx == null) {
            log.error("微信通道未找到{}",weChatId);
            return;
        }
        sendTextMsg(chx, friendWeChatId, byteString);
    }
    
    
    /**
     * 给用户发文字消息
     * @param ctx
     * @param friendWeChatId
     * @param content
     */
    public static void sendMessage(ChannelHandlerContext ctx,String friendWeChatId,String content) {
        log.info("发送消息......{}",content);
        ByteString byteString = ByteString.copyFromUtf8(content);
        if (ctx == null) {
            log.error("通道未找到");
            return;
        }
        sendTextMsg(ctx, friendWeChatId, byteString);
    }

	private static void sendTextMsg(ChannelHandlerContext ctx, String friendWeChatId, ByteString byteString) {
		//转发给手机端
        TalkToFriendTaskMessage.Builder bd = TalkToFriendTaskMessage.newBuilder();
        bd.setContent(byteString);
        bd.setContentType(EnumContentType.Text);
        //发给谁
        bd.setFriendId(friendWeChatId);
        bd.setMsgId(MsgIdBuilder.getId());
        TalkToFriendTaskMessage req2 = bd.build();
        MessageUtil.sendMsg(ctx, EnumMsgType.TalkToFriendTask, null, null, req2);
	}

   
}
